/*java jdk 1.7+
# -*- coding: utf-8 -
'''
Created on 2020年05月10日
@author: zxyong 13738196011
'''*/

package com.zxy.interfaceReflect;

import java.awt.Desktop;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

import com.zxy.common.Auto_Command;
import com.zxy.common.Com_Fun;
import com.zxy.common.Com_Para;

//关闭主进程并重启，暂时未支持linux
public class B01_RESTPM_IN implements com.zxy.common.ParamName_InterFace
{
	public StringBuilder	strResult		= new StringBuilder();
	public String			user_main_id			= null;
	public String			session_id		= null;
	public String			param_name		= null;
	// 执行命令密码 hr#$!23AC+时间格式yyyyMMddHH
	public String			param_value1	= null;
	// 关闭进程所属路径如 E:\apache-tomcat-7.0.94
	public String			param_value2	= null;
	// 启动程序地址如 E:\apache-tomcat-7.0.94\bin\startup.bat
	public String			param_value3	= null;
	public String			param_value4	= null;
	public String			param_value5	= null;
	public String			param_value6	= null;
	public String			param_value7	= null;
	public String			param_value8	= null;
	public String			param_value9	= null;
	public String			param_value10	= null;

	public void init_start()
	{
		String strTemParamValue1;
		try
		{
			strTemParamValue1 = Com_Fun.s_decode(this.param_value1,Com_Para.U_CODE);
			if(!strTemParamValue1.equals("hr#$!23AC"
					+ Com_Fun.DateTimeStr("yyyyMMddHH")))
				this.strResult.append(Com_Fun.get_ope_result(param_name,"0","error:password error"));
			else
			{
				String osName = System.getProperty("os.name");
				String strSuc = "";
				if(osName.startsWith("Windows"))
				{
					strSuc = KillPro("java");
					if(strSuc.equals(""))
						this.strResult.append(Com_Fun.get_ope_result(param_name,"1","success"));
					else
						this.strResult.append(Com_Fun.get_ope_result(param_name,"0",strSuc));
				}
			}
		}
		catch(Exception e)
		{
			this.strResult.append(Com_Fun.get_ope_result(param_name,"0",e.getMessage()));
			Com_Fun.debug_in(e,this.toString());
		}
	}

	public String KillPro(String prefix)
	{
		boolean bFlag = false;
		String strError = "";
		// 声明文件读取流
		BufferedReader out = null;
		BufferedReader br = null;
		try
		{
			// 创建系统进程
			ProcessBuilder pb = new ProcessBuilder("tasklist");
			Process p = pb.start();
			// 读取进程信息
			out = new BufferedReader(new InputStreamReader(new BufferedInputStream(p.getInputStream()),Charset.forName("GB2312")));
			br = new BufferedReader(new InputStreamReader(new BufferedInputStream(p.getErrorStream())));
			// kill进程
			String strTemParamValue2 = Com_Fun.s_decode(this.param_value2,Com_Para.U_CODE);
			// 启动该进程
			String strTemParamValue3 = Com_Fun.s_decode(this.param_value3,Com_Para.U_CODE);

			// 创建集合 存放 进程+pid
			List list = new ArrayList();
			// 读取
			String ostr;
			while(!java.util.Objects.equals((ostr = out.readLine()),null))
			{
				// 将读取的进程信息存入集合
				list.add(ostr);
			}
			// 遍历所有进程
			for(int i = 3;i < list.size();i++)
			{
				// 必须写死,截取长度,因为是固定的
				String process = String.valueOf(list.get(i)).substring(0,25).trim(); // 进程名
				String pid = String.valueOf(list.get(i)).substring(25,35).trim(); // 进程号
				// Process proc = Runtime.getRuntime().exec(process);
				// 匹配指定的进程名,若匹配到,则立即杀死
				if(process.toLowerCase().indexOf(prefix) == 0)
				{
					String strCmdPath = "wmic process " + pid
							+ " get Name,executablepath";
					Auto_Command ac = new Auto_Command();
					ac.strResult = "";
					ac.useLinuxNoResult(strCmdPath);
					String[] strAry = ac.strResult.split("\r\n");
					if(strAry.length < 3)
						strError = "";
					else
					{
						String[] strAryRes = strAry[2].split("  ");
						strCmdPath = "wmic process " + pid + " get CommandLine";
						ac = new Auto_Command();
						ac.strResult = "";
						ac.useLinuxNoResult(strCmdPath);
						strAry = ac.strResult.split("\r\n");

						if(ac.strResult.indexOf(strTemParamValue2) != -1)
						{
							Runtime.getRuntime().exec("taskkill /F /PID "
									+ pid);
							// 延迟5秒钟执行
							Thread.sleep(5000);
							RunCmd(strTemParamValue3);
							bFlag = true;
							break;
						}
					}
				}
			}

			// 若有错误信息 即打印日志
			String estr = br.readLine();
			if(!java.util.Objects.equals(estr,null))
			{
				System.out.println("errro==>" + estr);
			}else
			{
				if(!bFlag)
				{
					RunCmd(strTemParamValue3);
					bFlag = true;
				}
			}
		}
		catch(IOException e)
		{
			Com_Fun.debug_in(e,this.toString());
		}
		finally
		{
			// 关流
			try
			{
				if(!java.util.Objects.equals(out,null))
				{
					out.close();
				}
			}
			catch(IOException e)
			{
			}

			try
			{
				if(!java.util.Objects.equals(br,null))
				{
					br.close();
				}
			}
			catch(IOException e)
			{
			}
			return strError;
		}
	}

	private void RunCmd(String strTemParamValue3) throws IOException, InterruptedException
	{
		Desktop.getDesktop().open(new File(strTemParamValue3));
		/*
		 * Runtime.getRuntime().exec("cmd /c start /b "+strTemParamValue3);
		 * Runtime.getRuntime().exec(strTemParamValue3); Process ps =
		 * Runtime.getRuntime().exec("cmd.exe /C start /b "+strTemParamValue3);
		 * ps.waitFor(); int n = ps.exitValue();
		 * System.out.println("==>"+String.valueOf(n));
		 */
	}

	public StringBuilder getStrResult()
	{
		return strResult;
	}

	public void setStrResult(StringBuilder strResult)
	{
		this.strResult = strResult;
	}

	public String getUser_main_id()
	{
		return user_main_id;
	}

	public void setUser_main_id(String user_main_id)
	{
		this.user_main_id = user_main_id;
	}

	public String getSession_id()
	{
		return session_id;
	}

	public void setSession_id(String session_id)
	{
		this.session_id = session_id;
	}

	public String getParam_name()
	{
		return param_name;
	}

	public void setParam_name(String param_name)
	{
		this.param_name = param_name;
	}

	public String getParam_value1()
	{
		return param_value1;
	}

	public void setParam_value1(String param_value1)
	{
		this.param_value1 = param_value1;
	}

	public String getParam_value2()
	{
		return param_value2;
	}

	public void setParam_value2(String param_value2)
	{
		this.param_value2 = param_value2;
	}

	public String getParam_value3()
	{
		return param_value3;
	}

	public void setParam_value3(String param_value3)
	{
		this.param_value3 = param_value3;
	}

	public String getParam_value4()
	{
		return param_value4;
	}

	public void setParam_value4(String param_value4)
	{
		this.param_value4 = param_value4;
	}

	public String getParam_value5()
	{
		return param_value5;
	}

	public void setParam_value5(String param_value5)
	{
		this.param_value5 = param_value5;
	}

	public String getParam_value6()
	{
		return param_value6;
	}

	public void setParam_value6(String param_value6)
	{
		this.param_value6 = param_value6;
	}

	public String getParam_value7()
	{
		return param_value7;
	}

	public void setParam_value7(String param_value7)
	{
		this.param_value7 = param_value7;
	}

	public String getParam_value8()
	{
		return param_value8;
	}

	public void setParam_value8(String param_value8)
	{
		this.param_value8 = param_value8;
	}

	public String getParam_value9()
	{
		return param_value9;
	}

	public void setParam_value9(String param_value9)
	{
		this.param_value9 = param_value9;
	}

	public String getParam_value10()
	{
		return param_value10;
	}

	public void setParam_value10(String param_value10)
	{
		this.param_value10 = param_value10;
	}
}
